package cn.kivensoft.xlsx.reader;

import java.math.BigDecimal;
import java.util.Date;

public class Cell {
	private Row row;
	private Column column;
	private NumberFormat format = NumberFormat.General;
	private String value;

	public Cell(String value) {
		this.value = value;
	}

	public Row getRow() {
		return row;
	}

	public void setRow(Row value) {
		row = value;
	}

	public Column getColumn() {
		return column;
	}

	public void setColumn(Column value) {
		column = value;
	}

	public NumberFormat getFormat() {
		return format;
	}

	public void setFormat(NumberFormat value) {
		format = value;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public Integer getAsInteger() {
		if (value == null || value.isEmpty()) return null;
		try {
			return Integer.parseInt(value);
		} catch (Exception e) {
			return null;
		}
	}

	public Double getAsDouble() {
		if (value == null || value.isEmpty()) return null;
		try {
			return Double.parseDouble(value);
		} catch (Exception e) {
			return null;
		}
	}

	public BigDecimal getAsDecimal() {
		if (value == null || value.isEmpty()) return null;
		try {
			return new BigDecimal(value);
		} catch (Exception e) {
			return null;
		}
	}
	
	public Date getAsDate() {
		if (value == null || value.isEmpty()) return null;
		Date d = Utilities.strToDate(value);
		return d != null ? d : Utilities.toDate(value, row.getSheet()
				.getWorkbook().getBaseYear());
	}

	public Date getAsTime() {
		if (value == null || value.isEmpty()) return null;
		Date d = Utilities.strToTime(value);
		return d != null ? d : Utilities.toTime(value);
	}

	public Date getAsDateTime() {
		if (value == null || value.isEmpty()) return null;
		Date d = Utilities.strToDateTime(value);
		return d != null ? d : Utilities.toDateTime(value, row.getSheet()
				.getWorkbook().getBaseYear());
	}

}
